package jp.co.systemi.asp.workflow.apply;

import java.util.List;
import java.util.logging.Logger;

import jp.co.systemi.asp.utils.DModalWindow;
import jp.co.systemi.asp.workflow.ICallback;
import jp.co.systemi.asp.workflow.Main;
import jp.co.systemi.asp.workflow.data.Approver;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.datanucleus.util.StringUtils;

public class ApplyDialog extends WebPage {
    @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(ApplyDialog.class.getName());
    private String memo = "";
    /** フィードバック */
    protected FeedbackPanel feedback = new FeedbackPanel("wi_feedback");

    public ApplyDialog(final Main mainPage, final DModalWindow window) {
        init(mainPage, window);
    }

    @SuppressWarnings("unchecked")
    public void init(final Main mainPage, final DModalWindow window) {
        feedback.setOutputMarkupId(true);
        add(feedback);

//        final List<User> users = UserPeer.doSearch(null);

        Form<?> form = new Form("wi_form");
        this.add(form);
        form.add( new TextArea<String>("wi_memo", new PropertyModel<String>(this, "memo")));
        form.add(new AjaxButton("wi_OK") {
            private static final long serialVersionUID = 5873629255088464587L;
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                PageParameters pars = new PageParameters();
                pars.add("apply", "OK");
                String msg = ((ICallback)mainPage).getEmptyMessage(pars);
                if(StringUtils.isEmpty(memo) && !StringUtils.isEmpty(msg)){
                    error(msg);
                    target.addComponent(feedback);
                    return;
                }
//                pars.add("memo", memo);
//                ((ICallback)mainPage).setResult(pars);
                window.setRetValue(memo);
                window.setWindowClosedCallback(window.getOKActionCallback());
                window.close(target);
            }

        });
        form.add(new AjaxButton("wi_Cancel") {
            private static final long serialVersionUID = 5873629255088464587L;
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                window.setWindowClosedCallback(window.getCancelActionCallback());
                window.close(target);
            }
        });
        List<Approver> approvers = (List<Approver>)(((ICallback)mainPage).getDefault(null));
        form.add(new ListView<Approver>("wi_trApprover", approvers) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<Approver> item) {
                Approver user = item.getModelObject();
                item.add(new Label("wi_seq", new PropertyModel<String>(user,
                        "seq")));
                item.add(new Label("wi_role", new PropertyModel<String>(user,
                        "role")));
                item.add(new Label("wi_userName", new PropertyModel<String>(
                        user, "userName")));
                item.add(new Label("wi_groupName", new PropertyModel<String>(
                        user, "groupName")));

            }

        });
    }
}
